﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;
//using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;

namespace BesAsm.Emgaats.Core
{
  public partial class ModelStatusControl : UserControl //, ICommand, IToolControl
  {
    //private ICompletionNotify _completionNotify = null;    
    private EmgaatsCoreExtension _emgaatsExtension;
    private Model _model;
    private string _hoverText = "No model loaded";

    /// <summary>
    /// The ESRI PROG_ID for this control
    /// </summary>
    public const string PROG_ID = "BesAsm.Emgaats.EmgaatsUI.ModelStatusControl";

    /// <summary>
    /// Initializes a new instance of the <see cref="ModelStatusControl"/> class.
    /// </summary>
    public ModelStatusControl()
    {
      InitializeComponent();
    }
    /*
    #region ICommand Members

    /// <summary>
    /// The bitmap that is used as the icon on this command.
    /// </summary>
    public int Bitmap
    {
      get { return 0; }
    }

    /// <summary>
    /// The caption of this command.
    /// </summary>
    public string Caption
    {
      get { return "Current EMGAATS Model"; }
    }

    /// <summary>
    /// The name of the category with which this command is associated.
    /// </summary>
    public string Category
    {
      //Do not assign value here per http://resources.esri.com/help/9.3/arcgisdesktop/arcobjects/esrisystemui/itoolcontrol.htm
      get { return string.Empty; }
    }

    /// <summary>
    /// Indicates if this command is checked.
    /// </summary>
    public bool Checked
    {
      get { return false; }
    }

    /// <summary>
    /// The help context ID associated with this command.
    /// </summary>
    public int HelpContextID
    {
      get { return 0; }
    }

    /// <summary>
    /// The name of the help file associated with this command.
    /// </summary>
    public string HelpFile
    {
      get { return string.Empty; }
    }

    /// <summary>
    /// The statusbar message for this command.
    /// </summary>
    public string Message
    {
      get { return _hoverText; }
    }

    /// <summary>
    /// Occurs when this command is clicked.
    /// </summary>
    public void OnClick()
    {
      return;
    }

    /// <summary>
    /// Occurs when this command is created.
    /// </summary>
    /// <param name="hook"></param>
    public void OnCreate(object hook)
    {
      IApplication arcApp;
      if (hook != null)
        arcApp = hook as IApplication;
      else
        return;

      _emgaatsExtension = EmgaatsExtension.GetExtension();

      _model = Mediator.Model;
      Mediator.ModelLoaded += new EventHandler(ModelStatusControl_ModelLoaded);
      _model.ModelStateChanged += new ModelStateChangedHandler(_model_ModelStateChanged);
      SetStatusText();
    }

    /// <summary>
    /// The tooltip for this command.
    /// </summary>
    public string Tooltip
    {
      get
      {
        return _hoverText;
      }
    }

    #endregion

    #region IToolControl Members

    public bool OnDrop(esriCmdBarType barType)
    {
      if (barType == esriCmdBarType.esriCmdBarTypeToolbar)
        return true;
      return false;
    }

    public void OnFocus(ICompletionNotify complete)
    {
      _completionNotify = complete;      
    }

    public int hWnd
    {
      get { return this.Handle.ToInt32(); }
    }

    #endregion
    */
    void ModelStatusControl_ModelLoaded(object sender, EventArgs e)
    {
      _model = Mediator.Model;
      _model.ModelStateChanged += new ModelStateChangedHandler(_model_ModelStateChanged);
      SetStatusText();
    }

    void _model_ModelStateChanged(object sender, ModelStateChangedArgs e)
    {
      _model = e.Model;
      SetStatusText();
    }

    private void SetStatusText()
    {
      if (_model == null || _model.State == ModelState.Null)
      {
        txtCurrentModel.Text = "<none>";
        _hoverText = "No model loaded";
      }
      else
      {
        txtCurrentModel.Text = _model.Name;
        _hoverText = "Model State: " + _model.State.ToString();
      }
    } 

    private void ModelStatusControl_Paint(object sender, PaintEventArgs e)
    {
      panel1.Refresh();
    }

    private void panel1_Leave(object sender, EventArgs e)
    {
      //if (_completionNotify != null)
        //_completionNotify.SetComplete();      
    }

    
  }
}
